package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.IUserService;
import com.tarena.lbs.common.basic.converter.UserConverter;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.basic.param.UserParam;
import com.tarena.lbs.pojo.basic.param.UserStoreParam;
import com.tarena.lbs.pojo.basic.query.UserQuery;
import com.tarena.lbs.pojo.basic.query.UserStoreQuery;
import com.tarena.lbs.pojo.basic.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "用户管理")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private UserConverter userConverter;

    @PostMapping("/admin/basic/user/list")
    @ApiOperation("获取用户列表-分页")
    public Result<PageResult<UserVO>> getAll(@RequestBody UserQuery userQuery) {
        BasePageList<UserBO> userBos = userService.userList(userQuery);
        PageResult<UserVO> page = userConverter.assemblePageVOS(userBos,userQuery);
        return new Result<>(page);
    }

    @GetMapping("/admin/basic/user/detail/{id}")
    @ApiOperation("用户详情")
    public Result<UserVO> userDetail(@PathVariable("id") Integer userId) {
        UserBO userBO = userService.getDetail(userId);
        return new Result<>(userConverter.bo2vo(userBO));
    }

    @GetMapping("/basic/user/detail")
    @ApiOperation("前台:用户详情")
    public Result<UserVO> userDetail() {
        UserBO userBO = userService.getDetail(null);
        return new Result<>(userConverter.bo2vo(userBO));
    }

    @PostMapping("/basic/user/interest")
    @ApiOperation("关注用户")
    public Result userInterest(@RequestBody UserStoreParam userStoreParam) throws Exception {
        userService.userInterest(userStoreParam);
        return Result.success();
    }

    @PostMapping("/basic/user/interest/list")
    @ApiOperation("用户关注列表")
    public Result<PageResult<UserBO>> userInterestStoreList(@RequestBody UserStoreQuery userStoreQuery) throws Exception {
        PageResult<UserBO> result = userService.userInterestList(userStoreQuery);
        return new Result<>(result);
    }

    @PostMapping("/basic/user/info/save")
    @ApiOperation("用户修改资料")
    public Result<Integer> userSaveInfo(@RequestBody UserParam userParam) throws Exception {
        Integer userId = userService.saveUserInfo(userParam);
        return new Result<>(userId);
    }

    @PostMapping("/basic/user/info/register")
    @ApiOperation("用户注册")
    public Result<Integer> userRegister(@RequestBody UserParam userParam) throws Exception {
        Integer userId = userService.saveUserInfo(userParam);
        return new Result<>(userId);
    }

}
